﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Roguelike
{
    public enum displayTypes { text, tile }

    public class ScreenManager
    {
        // Max dimensions of map; these are temporary test values
        int mapX = 6;
        int mapY = 6;

        // Represents just the map tiles sans entities
        public tileType[,] map { get; set; }

        // The exact type of map renderer is decided in the constructor
        IMapRenderer renderer;

        // Keeps track of whether the game is displaying as text or tiles
        public displayTypes displayType;

        /// <summary>
        /// Constructor for ScreenManager
        /// </summary>
        public ScreenManager()
        {
            // This will be configurable later, but I'll set it here for now
            displayType = displayTypes.text;

            // Creates a MapRenderer based on the display type set in GameManager
            // This should move out of the constructor later if you can change display type in-game
            if (displayType == displayTypes.text)
            {
                renderer = new TextMapRenderer(mapX, mapY);
            }
        }

        /// <summary>
        /// Generates individual level maps
        /// </summary>
        public void GenerateMap()
        {
            // Temporary test map until level generation is implemented
            map = new tileType[,]
            { { tileType.wall , tileType.wall , tileType.wall , tileType.wall , tileType.wall , tileType.wall },
              { tileType.wall , tileType.floor, tileType.floor, tileType.floor, tileType.pit  , tileType.wall },
              { tileType.wall , tileType.floor, tileType.floor, tileType.floor, tileType.floor, tileType.wall },
              { tileType.wall , tileType.floor, tileType.floor, tileType.water, tileType.water, tileType.wall },
              { tileType.wall , tileType.pit  , tileType.floor, tileType.water, tileType.water, tileType.wall },
              { tileType.wall , tileType.wall , tileType.wall , tileType.wall , tileType.wall , tileType.wall } };

            renderer.GenerateGraphicsMap();
        }

        /// <summary>
        /// Calls the renderer's UpdateGraphicsMap method
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="oldX"></param>
        /// <param name="oldY"></param>
        /// <param name="newX"></param>
        /// <param name="newY"></param>
        /// <param name="entityExists"></param>
        public void UpdateGraphicsMap(Entity entity, int oldX, int oldY, int newX, int newY, bool entityExists = true)
        {
            renderer.UpdateGraphicsMap(entity, oldX, oldY, newX, newY, entityExists);
        }

        /// <summary>
        /// Returns the ID of the tile at the given x, y coordinates
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public tileType GetTile(int x, int y)
        {
            return map[y, x];
        }

        /// <summary>
        /// Initializes screen when the game starts
        /// </summary>
        public void InitializeScreen()
        {
            // This will be fancier eventually
            GenerateMap();
            DrawMap();
        }

        /// <summary>
        /// Calls the MapRenderer DrawMap function
        /// </summary>
        public void DrawMap()
        {
            renderer.DrawMap();
        }
    }
}
